package de.gameduell.framework.game.billard.controller {
	import de.gameduell.framework.game.billard.model.Ball;
	import de.gameduell.framework.game.billard.model.Cushion;
	import de.gameduell.framework.game.billard.model.Hole;
	
	import org.cove.ape.Group;
	import org.cove.ape.Vector;
	
	import flash.display.Sprite;	

	/**
	 * @author cbra
	 */
	public class HoleEngine extends BillardEngine {
		private static const HOLE_1:Array = new Array(29,28,6,7);
		private static const HOLE_2:Array = new Array(19,18,9,8);
		private static const HOLE_3:Array = new Array(21,10,11,20);
		private static const HOLE_4:Array = new Array(23,22,12,13);
		private static const HOLE_5:Array = new Array(25,24,14,15);
		private static const HOLE_6:Array = new Array(27,26,16,17);
		private static const HOLES:Array = new Array(HOLE_1,HOLE_2,HOLE_3,HOLE_4,HOLE_5,HOLE_6);
		
		private var _id:int;
		private var _content:Sprite;
		private var _hole:Hole;
		private var _target:Vector;

		public function HoleEngine(aContent:Sprite,aHole:Hole,aId:int) {
			super();
			_id = aId;
			_content = aContent;
			_hole = aHole;
			init(0.3);
			damping = 0.99;	
			container = new Sprite();
			addGroup( new Group(this, true));
			createCushions();
		}
		
		public function calcHoleBallPositions():void {
			var counter:Number = 1;
			var maxSteps:Number = 300;
			
			for(var j in balls) {
				var ball:Ball = balls[j];
				for(var i:int = 0;i < maxSteps;i++) {
					counter++;
					stepOptimized();

					if(counter < maxSteps) {
						var factor:Number = counter / maxSteps;
						var velocity:Vector = ball.velocity;
						ball.position = Vector.interpolate(ball.position, _hole.position, factor);
						if(ball.position.distance(_hole.position) < 1){
							ball.fallen = true;
							break;			
						}
						ball.velocity = velocity;
					}
					ball.createStep();			
				}
				finallMove(ball);		
				ball.fixed = true;
				ball.collidable = false;	
				ball.removed = true;
				ball.createStep();		
			}
			removeBalls();
		}
		
		private function finallMove(ball:Ball):void{
			var counter:Number = 1;
			var maxSteps:Number = 300;
			var lastPos:Vector = ball.position.clone();
			for(var i:int = 0;i < maxSteps;i++) {
				counter++;
				if(counter < maxSteps) {
					var factor:Number = counter / maxSteps;
					var delta:Vector = ball.position.minus(lastPos).mult(5);
					lastPos = ball.position.clone();
					ball.position = Vector.interpolate(ball.position.clone(), _target, factor);
					ball.velocity = delta;
					ball.convertTranslationToAngleShift(1);
				}else {
					ball.removed = true;
				}
				ball.createStep();			
			}
		}	
		
		private function createCushions():void{
			var cushions:Array = HOLES[_id];
			for(var i:* in cushions){
				var cushion:Sprite = _content.getChildByName("cushion_" + cushions[i]) as Sprite;
				Cushion.createParticle(this,cushion);		
				if(i == 0){
					_target = new Vector(cushion.x,cushion.y);
				}
			}
		}
		
		
	}
}
